package com.example.qxfw.common.util;

import com.example.qxfw.common.entity.qx.Weather;
import com.example.qxfw.ybzz.service.WeatherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 天气代码，天气互相转换
 */
@Component
public class WeatherUtil {


    @Autowired
    private WeatherService weatherService;

    private  Map<String,String> weatherToName = new HashMap<>();
    private  Map<String,String> nameToWeather = new HashMap<>();

    private  Map<String,String> windToName = new HashMap<>();
    private  Map<String,String> nameToWind = new HashMap<>();

    private  Map<String,String> winddToName = new HashMap<>();
    private  Map<String,String> nameToWindd = new HashMap<>();

    private void initData(){
        List<Weather> weathers = weatherService.getWeather();
        for (Weather weather : weathers) {
            weatherToName.put(weather.getWeatherid(),weather.getWeather());
            nameToWeather.put(weather.getWeather(),weather.getWeatherid());
        }

        List<Weather> weathers1 = weatherService.getWind();
        for (Weather weather : weathers1) {
            windToName.put(weather.getWeatherid(),weather.getWind());
            nameToWind.put(weather.getWind(),weather.getWeatherid());
        }

        List<Weather> weathers2 = weatherService.getWindd();
        for (Weather weather : weathers2){
            winddToName.put(weather.getWeatherid(),weather.getWindd());
            nameToWindd.put(weather.getWindd(),weather.getWeatherid());
        }
    }

    /**
     * 获取天气名称
     * @param weatherId
     * @return
     */
    public String getWeatherName(String weatherId){
        initData();
        return weatherToName.get(weatherId);
    }

    /**
     * 获取风力名称
     * @param weatherId
     * @return
     */
    public String getWindName(String weatherId){
        initData();
        return windToName.get(weatherId);
    }

    /**
     * 获取风向名称
     * @param weatherId
     * @return
     */
    public String getWinddName(String weatherId){
        initData();
        return winddToName.get(weatherId);
    }

    /**
     * 获取天气id
     * @param weather
     * @return
     */
    public String getWeatherId(String weather){
        initData();
        return nameToWeather.get(weather);
    }

    /**
     * 获取风力id
     * @param wind
     * @return
     */
    public String getWindId(String wind){
        initData();
        return nameToWind.get(wind);
    }

    /**
     * 获取风向id
     * @param windd
     * @return
     */
    public String getWinddId(String windd){
        initData();
        return nameToWindd.get(windd);
    }


    /**
     * 风速转风力
     * @param fengsu
     * @return
     */
    public String FengSuToFengJi(String fengsu)
    {
        initData();
        String strFs = fengsu;
        if (fengsu != "")
        {
            double fs = Double.parseDouble(fengsu);
            if (fs >= 0 && fs <= 3.3)
            {
                strFs = "0";
            }
            else if (fs <= 5.5)
            {
                strFs = "1";
            }
            else if (fs <= 7.9)
            {
                strFs = "2";
            }
            else if (fs <= 10.7)
            {
                strFs = "3";
            }
            else if (fs <= 13.8)
            {
                strFs = "4";
            }
            else if (fs <= 17.1)
            {
                strFs = "5";
            }
            else if (fs <= 20.7)
            {
                strFs = "6";
            }
            else if (fs <= 24.4)
            {
                strFs = "7";
            }
            else if (fs <= 28.4)
            {
                strFs = "8";
            }
            else if (fs > 28.4)
            {
                strFs = "9";
            }
        }
        return strFs;
    }

    /**
     * 得到风向ID信息
      * @param strFx
     * @return
     */
    public String GetFxIDByValue(String strFx)
    {
        initData();
        if (strFx != "")
        {
            double j = Double.parseDouble(strFx);
            if (j > 202.5 && j < 247.5)
            {
                strFx = "西南风";
            }
            else if (j > 292.5 && j < 360)
            {
                strFx = "西北风";
            }
            else if (j > 112.5 && j < 157.5)
            {
                strFx = "东南风";
            }
            else if (j > 22.5 && j < 67.5)
            {
                strFx = "东北风";
            }
            else if (j > 157.5 && j < 202.5)
            {
                strFx = "南风";
            }
            else if (j > 0 && j < 22.5)
            {
                strFx = "北风";
            }
            else if (j > 67.5 && j < 112.5)
            {
                strFx = "东风";
            }
            else if (j > 247.5 && j < 292.5)
            {
                strFx = "西风";
            }
            else
            {
                strFx = "东风";
            }
        }
        strFx = getWinddId(strFx);

        return strFx;
    }

}
